<?php
require_once("mysql.inc.php");
//require_once("checkLogin.inc.php");

if(isset($_GET["query"])) {
	searchUser($_GET["query"]);
}

function searchUser($query) {

	$query = mysql_real_escape_string($query);
	
	$result = mysql_query("SELECT * FROM `users` WHERE `username` LIKE '%$query%' OR `group` LIKE '%$query%' ORDER BY `username` LIMIT 20") or die(mysql_error());
	$num = mysql_num_rows($result);
	
	$i = 1;
	$no_info = "Niet bekend";

	//create new dom document
	$doc = new DomDocument('1.0');
	$root = $doc->createElement("results");
	$doc->appendChild($root);
	
	//create first element containing amount of results
	$n = $doc->createElement("num");
	$n->appendChild($doc->createTextNode($num));
	$root->appendChild($n);
	
	while($fetch = mysql_fetch_array($result)) {
	
		$t = $doc->createElement("user$i");
		
		if($fetch["role"] == "TopBeheerder") {
		$admin = "true";
		$topadmin = "true";
		}
		else if($fetch["role"] == "Beheerder") {
			$admin = "true";
			$topadmin = "false";
		}
		else {
			$admin = "false";
			$topadmin = "false";
		}
		
		$username = $doc->createElement("name$i");
		if(!empty($fetch["username"])) {
			$username->appendChild($doc->createTextNode($fetch["username"]));
		}			
		else {
			$username->appendChild($doc->createTextNode($no_info));
		}		
		$t->appendChild($username);
		
		$group = $doc->createElement("group$i");
		if(!empty($fetch["group"])) {
			$group->appendChild($doc->createTextNode($fetch["group"]));
		}			
		else {
			$group->appendChild($doc->createTextNode($no_info));
		}		
		$t->appendChild($group);
		
		$a = $doc->createElement("admin$i");
		$a->appendChild($doc->createTextNode($admin));
		$t->appendChild($a);
		
		$ta = $doc->createElement("topAdmin$i");
		$ta->appendChild($doc->createTextNode($topadmin));
		$t->appendChild($ta);
		
		$root->appendChild($t);
		
		$i++;
	}
	
	echo $doc->saveXML();
}
?>